using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

namespace StudentManagementApplication.DAO
{
    public class FeeTypeDAO : BaseDAO
    {
        public FeeTypeDAO()
        {
        }

        private Entities.FeeType  CreateFeeTypeFromReader(IDataReader reader)
        {
            Entities.FeeType feeType = new Entities.FeeType();
            feeType.TypeID = (string)reader["cTypeID"];
            feeType.TypeName = (string)reader["vTypeName"];
            feeType.Fee = (int)reader["iFee"];
            return feeType;
        }

        public void InsertFeeType(Entities.FeeType  feeType)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcInsertFeeType", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@cTypeID", feeType.TypeID));
                command.Parameters.Add(new SqlParameter("@vTypeName", feeType.TypeName));
                command.Parameters.Add(new SqlParameter("@iFee", feeType.Fee));
                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw;
            }
        }

        public void UpdateFeeType(Entities.FeeType  feeType)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcUpdateFeeType", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@cTypeID", feeType.TypeID));
                command.Parameters.Add(new SqlParameter("@vTypeName", feeType.TypeName));
                command.Parameters.Add(new SqlParameter("@iFee", feeType.Fee));
                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw;
            }
        }

        public void DeleteFeeType(Entities.FeeType  feeType)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcDeleteFeeType", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@cTypeID", feeType.TypeID));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw;
            }
        }

        public Entities.FeeType  GetFeeTypeByID(string sTypeID)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcGetFeeTypeByID", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("cTypeID", sTypeID));

                connection.Open();
                Entities.FeeType  feeType  = null;
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                    {
                        feeType  = CreateFeeTypeFromReader(reader);
                    }
                    //else
                    //    throw;
                }
                return feeType;
            }
        }

        public List<Entities.FeeType> GetAllFeeTypes()
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcGetAllFeeTypes", connection);
                command.CommandType = CommandType.StoredProcedure;

                connection.Open();
                List<Entities.FeeType> feeTypes = new List<Entities.FeeType>();
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        feeTypes.Add(CreateFeeTypeFromReader(reader));
                    }
                }
                return feeTypes;
            }
        }

    }
}
